package com.ruoyi.web.controller.ocr.domain;

import com.ruoyi.web.controller.ocr.utils.Base64Util;
import com.ruoyi.web.controller.ocr.utils.FileUtil;
import com.ruoyi.web.controller.ocr.utils.HttpUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLEncoder;

/**
 * 测试 images/身份证正面.jpg 识别
 * 输出结果:
 * {"data":{"ret":[{"word_name":"姓名","word":"谭卢钢"},{"word_name":"民族","word":"毛南"},{"word_name":"住址","word":"广西环江毛南族自治县下南乡才门村石望屯7号"},{"word_name":"公民身份号码","word":"452724199807151615"},{"word_name":"出生","word":"19980715"},{"word_name":"性别","word":"男"}],"templateSign":"id_card_front","templateName":"大陆身份证正面","scores":1.0,"isStructured":true,"logId":"160234443554108","version":1},"error_code":0,"error_msg":"","log_id":"160234443554108"}
 */
public class App
{
    public static void main(String[] args) throws Exception
    {
        /**
         * 重要提示代码中所需工具类
         * FileUtil,Base64Util,HttpUtil,GsonUtils请从
         * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
         * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
         * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
         * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
         * 下载
         */
        // iocr识别apiUrl
        String recogniseUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise";


        String filePath = "E:\\images\\身份证正面.jpg";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            // 请求模板参数
            String recogniseParams = "templateSign=id_card_front&image=" + URLEncoder.encode(imgStr, "UTF-8");
            // 请求分类器参数
            String classifierParams = "classifierId=id_card_front&image=" + URLEncoder.encode(imgStr, "UTF-8");

            String accessToken = "24.3075314825e7bbbcd45eac4977b93b8b.2592000.1609395744.282335-22662812";
            // 请求模板识别
            String result = HttpUtil.post(recogniseUrl, accessToken, recogniseParams);

            System.out.println(result);

            // 将String转为org.json.JSONObject
            /**
             * result:{"data":{"ret":[{"word_name":"姓名","word":"谭卢钢"},{"word_name":"民族","word":"毛南"},{"word_name":"住址","word":"广西环江毛南族自治县下南乡才门村石望屯7号"},{"word_name":"公民身份号码","word":"452724199807151615"},{"word_name":"出生","word":"19980715"},{"word_name":"性别","word":"男"}],"templateSign":"id_card_front","templateName":"大陆身份证正面","scores":1.0,"isStructured":true,"logId":"160723112104534","version":1},"error_code":0,"error_msg":"","log_id":"160723112104534"}
             */
            // 得到json对象
            JSONObject jsonObject = new JSONObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            System.out.println(data);
            // 得到json数组
            JSONArray ret = data.getJSONArray("ret");
            System.out.println(ret);

            JSONObject retJSONObject = ret.getJSONObject(3);
            System.out.println(retJSONObject);

            // 将json数组返回给前端 业务中实现

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
